#!/usr/bin/python
#
# absynth.py
# Copyright (C) Nicola Bizzoca   2006  <hannibal_smith@users.sourceforge.net>
# Copyright (C) Simone Economo   2006  <ekoes@users.sourceforge.net>
# 
# Absynth  is free software.
# 
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 2 of the License, or (at your option)
# any later version.
# 
# main.py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with main.py.  If not, write to:
#	 The Free Software Foundation, Inc.,
#	 51 Franklin Street, Fifth Floor
#	 Boston, MA  02110-1301, USA.

import os, sys

try:
	import gtk, gtk.glade
except ImportError:
	print >> sys.stderr, "Absynth needs GTK 2.10 or above to run."
	sys.exit(1)
	
import conf as Config
import abcommon as Utils

from twisted.internet import gtk2reactor
gtk2reactor.install()
from twisted.internet import reactor

class XMLtree:
	_instance = None
	
	def __init__(self, widget = False):
		gladeFile = Config.GLADE_MAIN_WINDOW
		self.methods = (widget and gtk.glade.XML(gladeFile, widget)) \
					 or gtk.glade.XML(gladeFile)
					 
		self.connectSignals = self.methods.signal_autoconnect
		self.getWidget = self.methods.get_widget

class Widgets:
	_instance = None
	
	__list__ = {
		# Name 		 : Load at startup?
		"LocalView"	 : True,
		"RemoteView" : True,
		"AboutDialog": False,
		"SysTray"	 : True,
		"LogView"	 : True,
		"MainWindow" : True,
	}
	
	@staticmethod
	def selectWidget(name):
		try:
			Widgets.__list__[name]
			return sys.modules["widgets.%s" % name]
		except:
			return None
	
	@staticmethod
	def getList():
		return Widgets.__list__
	
	def __init__(self):
		self.gladeTree = XMLtree
	 	self.XMLtree = Utils.Invoker(self.gladeTree)
	 	
	def changeXMLtree(self, root):
		self.XMLtree = self.gladeTree(root)
		
	def connectEvents(self): pass
		
class Absynth:
	
	@staticmethod
	def __mainLoop(): 
		reactor.run()
	
	def __init__(self):
		self.__startup()
		self.__mainLoop()
	
	def __startup(self):
		for widget in Widgets.getList():
			if Widgets.getList()[widget] is False:
				continue
			else:
				Utils.Invoker(Widgets.selectWidget(widget).UI)
